<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: xajax.google.php 11 2013-09-21 11:43:11Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 11 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-21 11:43:11 +0000 (Sat, 21 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

include(DIR_WS_CLASSES.'class.google_taxonomy.php');
$google = new googleTaxonomy();

function get_exist_url($pid) {
	global $db;
	$ajax = new xajaxResponse();
	
	$get = $db->db_query("SELECT category_path FROM ".TABLE_GOOGLE_TAXONOMY." WHERE products_id = '".$pid."'");
	
	if($get->_numOfRows) {
		$ajax->script('$("#google_url").html("'.$get->fields['category_path'].'");');
		$ajax->script('$("#save_button").hide();');
	} elseif(empty($pid) || empty($get->fields['category_path']))
		$ajax->call('xajax_get_new_dropdown');

	return $ajax;	
}

function get_new_dropdown() {
	global $google;
	$ajax = new xajaxResponse();
	$c = '<table width="100%" border="0" id="selection" style="margin-left:20px" cellpadding="4">
			<tr>
				<td class="google_cat" id="sub_select" width="1">
					'.draw_pull_down_menu('google', $google->get_dropdown_data(), '', 'onchange="xajax_get_next_cat(this.value, \'\')" style="width:380px" class="selections"').'
				</td>
			</tr>
		</table>';
	
	$ajax->append('google_cat', 'innerHTML', $c);
	
	return $ajax;
}

function get_next_cat($value = '', $parent = '') {
	$ajax = new xajaxResponse();
	
	if(!empty($parent)) {
		$path = explode('|', $parent);
		$path[] = $value;
	} else
		$path[] = $value;

	if(sizeof($path) == 1) {
		$c .= '<tr><td width="1">'.drop_down('', $value, true).draw_hidden_field('path_0', $value).'</td></tr>'."\n";
		$c .= '<tr><td width="1">'.drop_down($value).'</td></tr>'."\n";
		$ajax->script('$("#save_button").addClass("save_google_button_on");');
		
	} else {
		$long = implode(' > ', $path);
		for ($i = 0; $i < sizeof($path); $i++) {
			$np .= ($i > 0 && $path[$i -1]!='' ? ' > ' : '').$path[$i -1];
			$c .= '<tr><td width="1">'.drop_down(substr($np, strlen($path[$i -1])), $path[$i], true).'</td></tr>'."\n";
		}
		if(drop_down($long, '', $id))
			$c .= '<tr><td width="1">'.drop_down($long, '', $id).'</td></tr>'."\n";
	}
	
	$j  = '$("#short_path").val("'.implode('|', $path).'");';
	$j .= '$("#google_url").html("'.implode(' > ', $path).'");';
	$ajax->script($j);
	$ajax->assign('selection', 'innerHTML', $c);
	
	return $ajax;
}

function drop_down($value = '', $default ='', $read_only = false) {
	global $google;
	$data = $google->get_dropdown_data($value);

	if(!empty($data)) {
		$d = draw_pull_down_menu('', $data, $default, ($read_only?'disabled="" ':'').'onchange="xajax_get_next_cat(this.value, $(\'#short_path\').val())" style="width:380px"');
		return $d;
	}
	return false;
}

function reset_cat($pid) {
	global $db;
	
	$ajax = new xajaxResponse();
	
	$check = $db->db_query("SELECT category_path FROM ".TABLE_GOOGLE_TAXONOMY." WHERE products_id = '".$pid."'");
	if($check->_numOfRows) {
		$db->db_query("DELETE FROM ".TABLE_GOOGLE_TAXONOMY." WHERE products_id = '".$pid."'");
		
		$ajax->insertAfter('cat_table', 'table', 'selection');
		$ajax->script('$("#selection").width(400).css({\'margin-left\':20}).attr("cellpadding","4");');
		$ajax->script('$("#save_button").fadeIn(800);');
		$ajax->call('xajax_message', 'Die Google-Kategorie wurde aus der Datenbank gelöscht.', 'warning');
	}
	
	$ajax->assign('google_url', 'innerHTML', '');
	$ajax->assign('short_path', 'value', '');
	
	$c = '<tr><td>'.drop_down().'</td></tr>'."\n";
	$ajax->assign('selection', 'innerHTML', $c);
	
	$ajax->script('$("#save_button").removeClass("save_google_button_on")');
	
	return $ajax;
}

function save_cat($url, $pid) {
	global $db;
	$ajax = new xajaxResponse();
	
	if(empty($url))
		return $ajax->call('xajax_message', 'Sie können keine Leere Google Kategorie abspeichern', 'error');
	
	$sql = array('products_id' => $pid,
				'category_path' => $url);
	
	$db->db_perform(TABLE_GOOGLE_TAXONOMY, $sql);
	$ajax->script('$("#save_button").removeClass("save_google_button_on")');
	$ajax->script('$("#selection").fadeOut(600,function(){$(this).remove();});');
	$ajax->script('$("#save_button").fadeOut();');	
	return $ajax->call('xajax_message', 'Diese neue Google Kategorie-URL wurde gespeichert.', 'success');
}

$xajax->registerFunction('get_exist_url');
$xajax->registerFunction('get_next_cat');
$xajax->registerFunction('reset_cat');
$xajax->registerFunction('save_cat');
$xajax->registerFunction('get_new_dropdown');
?>